package controllers

import (
	"net/url"
	"strings"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
)

type InputDiagnosisController struct {
	BaseController
}

func (c *InputDiagnosisController) Prepare() {
	//先执行
	c.BaseController.Prepare()
	c.checkLogin()
}

func (c *InputDiagnosisController) Index() {

	query_type := c.GetString("query_type")
	if c.Ctx.Input.IsPost() {
		kwd, _ := url.QueryUnescape(c.GetString("kwd"))
		switch query_type {
		case "presc_diagnosis": //医嘱诊断
			c.presc_diagnosis(kwd)
			break;
		case "presc_content": //医嘱内容
			c.presc_content(kwd)
			break;
		}
	} else {
		c.display("inputdiagnosis/"+query_type+".html", "inputdiagnosis/"+query_type+"_footerjs.html", "shared/layout_dialog.html")
	}
}

//调取常用诊断
func (c *InputDiagnosisController) presc_diagnosis(kwd string) {
	list := models.DoDiagnosisOftenList(kwd)
	c.jsonResult(enums.JRCodeSucc, "ok", list)
	c.StopRun()
}

//获取药品、检查数据
func (c *InputDiagnosisController) presc_content(kwd string) {
	//获取检查
	inspects_query := new(models.InspectsQueryParam)
	inspects_query.Offset = 0
	inspects_query.Limit = 100
	inspects_query.Filter = kwd
	inspects_query.IsStop = "0"
	inspects, _ := models.InspectsPageList(inspects_query)
	result := make(map[string]interface{})
	result["inspects"] = inspects

	//获取药品
	drug_query := new(models.ErpInventoryQueryParam)
	drug_query.Filter = kwd
	drug_query.Isnegative = "1" //必须有库存
	drug_query.Department = c.curUser.DepartmentId
	drug_query.Typeid = 70 //指定为药品
	drug := models.ErpInventoryList(drug_query)
	paramsPrice := new(models.InventoryPriceQueryParams)
	for k, _ := range drug {
		paramsPrice.DepId = drug[k].DepartmentId
		paramsPrice.Product = drug[k].Products
		paramsPrice.Snd = drug[k].Snd
		price := models.GetInventoryPriceForQueryParams(paramsPrice);
		if price.Id > 0 {
			drug[k].SuggestedPrice = price.SuggestedPrice
		}
	}

	result["drug"] = drug
	c.jsonResult(enums.JRCodeSucc, "ok", result)
	c.StopRun()
}

//添加常用诊断
func (c *InputDiagnosisController) DiagnosisAddOften() {
	title := c.GetString("title")
	if len(title) <= 0 {
		c.jsonResult(enums.JRCodeFailed, "参数为空", "")
	}
	split_title := strings.Split(title, ",")
	if err := models.DoDiagnosisOftenSave(split_title...); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "添加成功", "")
}

